package com.itboye.sunsun.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class SmsLoginActivity extends BaseActivity implements OnClickListener{

	
	
	private Button loginbutton;
	private Button yzmbutton;
	private EditText phoneedit;
	private EditText yzmedit;
	private TimeCount time;
	private String username;
	private RelativeLayout backrela;
	protected String code;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yzm_login);
		time=new TimeCount(60000, 1000, yzmbutton);
		username=phoneedit.getText().toString();
		initData();
	}
	public void initData()
	{
		
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yzmbutton:
			username=phoneedit.getText().toString();
			if(!username.equals(""))
			{
			   getyzm();
			   time.start();
			}else
			{
				Toast.makeText(SmsLoginActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.loginbutton:
			if(yzmedit.getText().toString().trim()==null){
				Toast.makeText(this, "请输入验证码", 0).show();
				return ;
			}
			login();
			break;
		case R.id.backrela:
			this.finish();
			break;

		default:
			break;
		}
		
	}
	public void getyzm()
	{
		MyJsonRequest<String>request= new MyJsonRequest.Builder<String>()
				 .apiVer("100")
				 .typeKey("BY_Message_send_sms")
				 .param("mobile", phoneedit.getText().toString())
				 .param("code_type", "5")
				 .requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());	
						code=response.toString();
					
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
						//	Toast.makeText(App.ctx, msg, 0).show();
						}else
						{
						  //	Toast.makeText(App.ctx, exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
	}
	public void login()
	
	{
		
		MyJsonRequest<PersonDataBean>request= new MyJsonRequest.Builder<PersonDataBean>()
				 .apiVer("100")
				 .typeKey("BY_User_loginByCode")
				 .param("mobile", phoneedit.getText().toString())
				 .param("code", yzmedit.getText().toString())
				 .requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());	
						//Toast.makeText(App.ctx, "登录成功", 0).show();
						
						SPUtils.put(App.ctx, null, SPContants.USERNAME, response.getUsername());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, response.getMobile());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD, response.getPassword());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						SPUtils.put(App.ctx, null, SPContants.EMAIL, response.getEmail());
						SPUtils.put(App.ctx, null, SPContants.NICK, response.getNickname());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD, response.getPassword());
						SPUtils.put(App.ctx, null, SPContants.USER_ID, response.getUid());
						SPUtils.put(App.ctx, null, SPContants.AVATAR_URL, response.getHead());
						
						
						
//						Intent intent=new Intent(SmsLoginActivity.this,MainActivity.class);
//						startActivity(intent);
						
						LoginController.setLoginState(new LoginedState());
						SmsLoginActivity.this.finish();
						
						//发送广播通知LoginActivity结束
						Intent intent1=new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						
						
						//发送广播通知更改头像
						Intent intent2=new Intent("changeavart");
						sendBroadcast(intent2);
						
					
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
							Toast.makeText(App.ctx,msg, 0).show();
						}else
						{
							Toast.makeText(App.ctx,exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
		
		
		
	}
}
